package dicapi

import (
	// "net/http"
	"encoding/json"
	"strconv"

	// "time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"

	// "github.com/jinzhu/gorm"

	"oxy_server/model"
	"oxy_server/module/log"
)

type (
	year struct {
		ID       bson.ObjectId `json:"_id" bson:"_id"`
		YearName string        `json:"yearname" bson:"yearname"`
		YearCode string        `json:"yearcode" bson:"yearcode"`
		Brand    string        `json:"brand" bson:"brand"`       //品牌
		CodeInfo []Code        `json:"codeinfo" bson:"codeinfo"` //品牌
	}
	insertyear struct {
		YearName string `json:"yearname" bson:"yearname"`
		YearCode string `json:"yearcode" bson:"yearcode"`
		Brand    string `json:"brand" bson:"brand"`       //品牌
		CodeInfo []Code `json:"codeinfo" bson:"codeinfo"` //品牌
	}
	Code struct {
		Code  string `json:"code" bson:"code"`
		Brand string `json:"brand" bson:"brand"` //品牌
	}
)

func GetYearListHandler(c echo.Context) error {
	currentpage, err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage, err := strconv.Atoi(c.QueryParam("perpage"))
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "year_dic"
	query, count, err := model.DB().QuerySort(nil, currentpage, perpage, "_id")
	if err != nil {
		log.Error("查询出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	ps := []year{}
	query.All(&ps)
	str, err := json.Marshal(ps)
	if err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":  string(str),
		"count": count,
	})
	return nil
}

func AddYearHandler(c echo.Context) error {
	y := &insertyear{}
	if err := c.Bind(y); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "year_dic"
	err := model.DB().Insert(y)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})
	return nil

}

func UpdateYearHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("UpdateYearHandler", id)

	y := &insertyear{}
	if err := c.Bind(y); err != nil {
		log.Error("解析参数错误", err)
		c.JSON(403, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	model.DB().Dbname = "OxyDic"
	model.DB().Document = "year_dic"
	err := model.DB().Update(bson.M{"_id": bson.ObjectIdHex(id)}, y)
	if err != nil {
		log.Error("插入出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}

	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil

}
func DelYearHandler(c echo.Context) error {
	id := c.QueryParam("id")
	log.Info("DelYearHandler", id)
	model.DB().Dbname = "OxyDic"
	model.DB().Document = "year_dic"
	err := model.DB().Remove(bson.M{"_id": bson.ObjectIdHex(id)})
	if err != nil {
		log.Error("删除出错", err)
		c.JSON(404, map[string]interface{}{
			"msg": "error",
		})
		return err
	}
	c.JSON(200, map[string]interface{}{
		"msg": "success",
	})

	return nil

}
